@echo off
if "%1%"=="start" (
	goto Start
) else (
	if "%1%"=="stop" ( 
		goto Stop
		) else (
		if "%1%"=="restart" ( 
			goto Restart
			) else ( 
			goto ShowHelp
		)
	)
)
goto End
:Start
	start /b uoampagent.exe -jar uoamp-agent-main-0.0.1-SNAPSHOT.jar
	echo uoampagent start success!
	echo please do not close this cmd window!
    goto install
	goto End

:Stop
	taskkill /im uoampagent.exe /f
	echo uoampagent stop success!
	echo please do not close this cmd window!
	goto End

:Restart
	taskkill /im uoampagent.exe /f
	start /b uoampagent.exe -jar uoamp-agent-main-0.0.1-SNAPSHOT.jar
	echo uoampagent restart success!
	echo please do not close this cmd window!
    goto install
	goto End

:ShowHelp
	echo please run bat like uoampagent.bat start/restart/stop.
	echo please do not close this cmd window!
	goto End

    
:install
    sc query "UoampAgentService" >nul
    if errorlevel 1060 goto self-starting
    goto End   

:self-starting
    set serviceName=UoampAgentService
    set programeName=uoampagent.bat
    set dir=%windir%\System32
    for /f "tokens=*" %%b in ('systeminfo^|find "x64"') do set BIT_FLAG=true
    if "%BIT_FLAG%"=="true"  (
        set dir=%windir%\SysWow64
    )

    copy %~dp0srvany\instsrv.exe %dir%
    copy %~dp0srvany\srvany.exe %dir%

    %dir%\instsrv.exe %serviceName% %dir%\srvany.exe
    echo instsrv Parameters! > ./uoampagent/uoampagentInstall.log

    set curExe=%~dp0%programeName%
    echo add Parameters! > ./MCAgent/uoampagentInstall.log

    set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\%serviceName%\Parameters\

    reg add %regpath% /v AppDirectory /t REG_SZ /d "%~dp0\" /f

    reg add %regpath% /v Application /t REG_SZ /d "%curExe%" /f 

    reg add %regpath% /v AppParameters /t REG_SZ /d "start" /f
    echo install service succss! > ./uoampagent/uoampagentInstall.log   
    goto End 
:End
@exit